package com.bdmc.web.controller.alarm;

import java.util.List;
import java.util.Arrays;

import com.bdmc.alarm.domain.AlarmRule;
import com.bdmc.alarm.domain.bo.AlarmRuleConfigBo;
import com.bdmc.alarm.service.IAlarmRuleConfigService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.alarm.domain.vo.AlarmRuleVo;
import com.bdmc.alarm.domain.bo.AlarmRuleBo;
import com.bdmc.alarm.service.IAlarmRuleService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 报警规则控制器
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/alarm/rule")
public class AlarmRuleController extends BaseController {

    private final IAlarmRuleService iAlarmRuleService;

    private final IAlarmRuleConfigService alarmRuleConfigService;

    /**
     * 查询报警规则列表
     */
    @SaCheckPermission("alarm:rule:list")
    @GetMapping("/list")
    public TableDataInfo<AlarmRuleVo> list(@Validated(QueryGroup.class) AlarmRuleBo bo, PageQuery pageQuery) {
        return iAlarmRuleService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出报警规则列表
     */
    @SaCheckPermission("alarm:rule:export")
    @Log(title = "报警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated AlarmRuleBo bo, HttpServletResponse response) {
        List<AlarmRuleVo> list = iAlarmRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "报警规则", AlarmRuleVo.class, response);
    }

    /**
     * 获取报警规则详细信息
     */
    @SaCheckPermission("alarm:rule:query")
    @GetMapping("/{alarmRuleId}")
    public AjaxResult<AlarmRuleVo> getInfo(@PathVariable("alarmRuleId") Integer alarmRuleId) {
        AlarmRuleVo alarmRuleVo = iAlarmRuleService.queryById(alarmRuleId);
        return AjaxResult.success(alarmRuleVo);
    }

    /**
     * 新增报警规则
     */
    @SaCheckPermission("alarm:rule:add")
    @Log(title = "报警规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AlarmRuleBo bo) {
        return toAjax(iAlarmRuleService.insertByBo(bo));
    }

    /**
     * 修改报警规则
     */
    @SaCheckPermission("alarm:rule:edit")
    @Log(title = "报警规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AlarmRuleBo bo) {
        return toAjax(iAlarmRuleService.updateByBo(bo));
    }

    /**
     * 删除报警规则
     */
    @SaCheckPermission("alarm:rule:remove")
    @Log(title = "报警规则" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{alarmRuleIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] alarmRuleIds) {
        return toAjax(iAlarmRuleService.deleteWithValidByIds(Arrays.asList(alarmRuleIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<AlarmRuleVo>> combo(AlarmRuleBo bo){
        return  AjaxResult.success(iAlarmRuleService.queryList(bo));
    }
}
